import { SelectUserComponentUser } from '@src/component/common/BaseSelectUser/model';
/* store */
import MsgModel from '@model/MsgModel'
import {
  useBaseSelectUserStoreCurrentServiceProviderNav,
  useBaseSelectUserStoreFetchServiceProviderList,
  useBaseSelectUserStoreFetchServiceProviderQualificationList,
  useBaseSelectUserStoreFetchServiceProviderTypeList,
  useBaseSelectUserStoreFetchServiceProviderUserList,
  useBaseSelectUserStoreFilterForm,
  useBaseSelectUserStoreLoadingMap,
  useBaseSelectUserStoreServiceProviderList,
  useBaseSelectUserStoreServiceProviderQualificationList,
  useBaseSelectUserStoreServiceProviderTypeList,
  useBaseSelectUserStoreUserCountMap,
  useBaseSelectUserStoreUserListMap,
} from '@src/component/common/BaseSelectUser/store'
/* hooks */
import { useBaseSelectUserServiceProviderListParams, useBaseSelectUserServiceProviderUserListParams } from '@src/component/common/BaseSelectUser/hooks/params'
/* model */
import { 
  BaseSelectUserErrorMessageEnum, 
  BaseSelectUserWarnMessageEnum, 
  SelectUserComponentServiceProvider 
} from '@src/component/common/BaseSelectUser/model'
/* util */
import Log from '@src/util/log'
import { message } from '@src/util/message'
import { isFalse, isNotEmpty } from '@src/util/type'
import { isDisabledSelectItem, setBaseSelectUserListChecked } from '@src/component/common/BaseSelectUser/util'
import log from '@src/util/log'
/* vue */
import { computed } from 'vue'

function useSelectUserServiceProviderUserList() {
  
  const { serviceProviderUserList } = useBaseSelectUserStoreUserListMap()
  const { serviceProviderUserCount } = useBaseSelectUserStoreUserCountMap()
  
  // 角色人员列表是否为空
  const isUserListEmpty = computed(() => {
    return serviceProviderUserList.value.length == 0 || serviceProviderUserCount.value == 0
  })
  
  // 角色人员列表是否全部加载完成
  const isUserListAllLoaded = computed(() => {
    return serviceProviderUserList.value.length >= serviceProviderUserCount.value
  })
  
  // 角色人员列表是否未全部加载完成
  const isUserListNotAllLoaded = computed(() => {
    return isFalse(isUserListAllLoaded.value)
  })
  
  // 是否禁用加载更多人员状态
  const isLoadmoreDisabled = computed(() => {
    
    // 当前人员数量为空时，不禁用加载状态可以加载更多
    if (isUserListEmpty.value) {
      return false
    }
    
    // 全部加载人员后，禁用加载
    return isUserListAllLoaded.value
    
  })
  
  const isLoadmoreNotDisabled = computed(() => isFalse(isLoadmoreDisabled.value))
  
  return {
    isUserListEmpty,
    isUserListAllLoaded,
    isLoadmoreDisabled,
    isLoadmoreNotDisabled,
    isUserListNotAllLoaded
  }
  
}

/** 
 * @description 获取选人组件 服务商列表
*/
function useFetchServiceProviderList() {
  
  const { setServiceProviderList } = useBaseSelectUserStoreServiceProviderList()
  const { setServiceProviderLoading, clearServiceProviderLoading } = useBaseSelectUserStoreLoadingMap()
  const { serviceProviderListParams } = useBaseSelectUserServiceProviderListParams()
  
  const { filterServiceProviderForm } = useBaseSelectUserStoreFilterForm()
  
  const fetchServiceProviderList = useBaseSelectUserStoreFetchServiceProviderList()
  
  const fetchServiceProviderListImpl = () => {
    
    setServiceProviderLoading()
    
    return (
      fetchServiceProviderList(serviceProviderListParams.value).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          let serviceProviderList = (result?.data || []) as SelectUserComponentServiceProvider[]
          
          if (
            isNotEmpty(filterServiceProviderForm.value.spList)
            || isNotEmpty(filterServiceProviderForm.value.spTypeList)
          ) {
            serviceProviderList = serviceProviderList.filter(serviceProvider => {
              return isFalse(isDisabledSelectItem(serviceProvider))
            })
          }
          
          setBaseSelectUserListChecked(serviceProviderList)
          
          setServiceProviderList(serviceProviderList)
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.ServiceProviderList)
        }
        
      }).finally(() => {
        clearServiceProviderLoading()
      })
    )
  }
  
  return fetchServiceProviderListImpl
}

/** 
 * @description 获取选人组件 服务商资质列表
*/
function useFetchServiceProviderQualificationList() {
  
  const { setServiceProviderQualificationList } = useBaseSelectUserStoreServiceProviderQualificationList()
  const fetchServiceProviderQualificationList = useBaseSelectUserStoreFetchServiceProviderQualificationList()
  
  const fetchServiceProviderQualificationListImpl = () => {
    return (
      fetchServiceProviderQualificationList().then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          const serviceProviderQualificationList = result?.data || []
          
          setServiceProviderQualificationList(serviceProviderQualificationList)
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.ServiceProviderList)
        }
        
      })
    )
  }
  
  return fetchServiceProviderQualificationListImpl
}

/** 
 * @description 获取选人组件 服务商下 人员列表
*/
function useFetchServiceProviderUserList() {
  
  const { serviceProviderUserList, setServiceProviderUserList } = useBaseSelectUserStoreUserListMap()
  const { setServiceProviderUserCount } = useBaseSelectUserStoreUserCountMap()
  const { isUserListNotAllLoaded, isLoadmoreDisabled } = useSelectUserServiceProviderUserList()
  const { setServiceProviderUserLoading, clearServiceProviderUserLoading } = useBaseSelectUserStoreLoadingMap()
  const { currentServiceProviderNav, setCurrentServiceProviderNav } = useBaseSelectUserStoreCurrentServiceProviderNav()
  
  const { serviceProviderUserListParams } = useBaseSelectUserServiceProviderUserListParams()
  
  const fetchServiceProviderUserList = useBaseSelectUserStoreFetchServiceProviderUserList()
  
  const fetchServiceProviderUserListImpl = (isInit: boolean = false) => {
    
    if (isLoadmoreDisabled.value && isFalse(isInit)) {
      Log.warn(BaseSelectUserWarnMessageEnum.ServiceProviderList, fetchServiceProviderUserListImpl.name)
      return Promise.resolve()
    }
    
    setServiceProviderUserLoading()
    
    return (
      fetchServiceProviderUserList(serviceProviderUserListParams.value).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          const userCount = result?.data?.userTotalCount || 0
          const serviceProviderChildList = result?.data?.tagChildren || []
          let userList = (result?.data?.userList || []) as SelectUserComponentUser[]
          
          const isFirstPage = serviceProviderUserListParams.value.pageNum == 1
          
          // 服务商人员列表没有全部加载完成，则合并新的数据
          if (isUserListNotAllLoaded.value) {
            userList = serviceProviderUserList.value.concat(userList)
          }
          
          setBaseSelectUserListChecked(userList)
          
          setServiceProviderUserList(userList)
          setServiceProviderUserCount(userCount)
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.ServiceProviderList)
        }
        
      }).finally(() => {
        clearServiceProviderUserLoading()
      })
    )
  }
  
  return fetchServiceProviderUserListImpl
}

/** 
 * @description 获取选人组件 服务商类型列表
*/
function useFetchServiceProviderTypeList() {
  
  const fetchServiceProviderTypeList = useBaseSelectUserStoreFetchServiceProviderTypeList()
  const { setServiceProviderTypeList } = useBaseSelectUserStoreServiceProviderTypeList()
  
  const fetchServiceProviderTypeListImpl = () => {
    return (
      fetchServiceProviderTypeList().then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          const serviceProviderTypeList = result?.data || []
          
          setServiceProviderTypeList(serviceProviderTypeList)
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.ServiceProviderTypeList)
        }
        
      })
    )
  }
  
  return fetchServiceProviderTypeListImpl
}

function useFetchServiceProviderListAndServiceProviderUserList() {
  
  const fetchServiceProviderList = useFetchServiceProviderList()
  const fetchServiceProviderUserList = useFetchServiceProviderUserList()
  
  const { serviceProviderList, setServiceProviderList } = useBaseSelectUserStoreServiceProviderList()
  const { setCurrentServiceProviderNav } = useBaseSelectUserStoreCurrentServiceProviderNav()
  const { resetPageNum } = useBaseSelectUserServiceProviderUserListParams()
  const { setServiceProviderUserCount } = useBaseSelectUserStoreUserCountMap()
  const { setServiceProviderUserList } = useBaseSelectUserStoreUserListMap()
  
  const { clearServiceProviderUserLoading } = useBaseSelectUserStoreLoadingMap()
  
  const fetchRoleListAndRoleUserList = (isResetCurrentServiceProviderNav: boolean = true) => {
    
    setServiceProviderList()
    
    fetchServiceProviderList().then(() => {
      
      const firstServiceProvider = serviceProviderList.value[0]
      const isDisabled = isDisabledSelectItem(firstServiceProvider)
      const isNotDisabled = isFalse(isDisabled)
      
      setServiceProviderUserCount()
      setServiceProviderUserList()
      
      if (isDisabled) {
        return
      }
      
      if (firstServiceProvider && isNotDisabled) {
        
        if (isResetCurrentServiceProviderNav) {
          setCurrentServiceProviderNav(firstServiceProvider)
        }
        
        resetPageNum()
        setServiceProviderUserCount()
        fetchServiceProviderUserList()
        
      } else {
        clearServiceProviderUserLoading()
      }
      
    })
    
  }
  
  return fetchRoleListAndRoleUserList
}

export {
  useSelectUserServiceProviderUserList,
  useFetchServiceProviderQualificationList,
  useFetchServiceProviderList,
  useFetchServiceProviderUserList,
  useFetchServiceProviderTypeList,
  useFetchServiceProviderListAndServiceProviderUserList
}
